package com.example.timerecorder;

import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class SearchFragment extends Fragment {

    private SearchViewModel mViewModel;

    private static final String TAG = "IndexFragment";
    private EditText searchText;
    private ListView searchResultList;
    private TextView noDataText;
    private ArrayList<Item> searchListItems;
    private SearchAdapter searchAdapter;

    public static SearchFragment newInstance() {
        return new SearchFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.search_fragment, container, false);
        searchText = view.findViewById(R.id.searchText);
        searchResultList = view.findViewById(R.id.searchResultList);
        noDataText = view.findViewById(R.id.nodataText);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(SearchViewModel.class);
        // TODO: Use the ViewModel

        //在数据库中查找记录
        DBManager dbManager = new DBManager(getActivity().getApplicationContext());

        //监听search输入内容
        searchText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String searchStr = editable.toString();
                if(searchStr.length()>0) {
                    searchListItems = dbManager.findRecord(searchStr);
                    Log.i(TAG, "onActivityCreated: 搜索查找");

                    //使用自定义适配器
                    searchAdapter = new SearchAdapter(
                            getActivity(),
                            R.layout.list_search,
                            searchListItems
                    );
                    searchResultList.setAdapter(searchAdapter);
                    //没有数据时显示
                    noDataText.setVisibility(View.VISIBLE);
                    searchResultList.setEmptyView(noDataText);
                }else{
                    //清除现有adpter中内容
                    searchResultList.setAdapter(null);
                    noDataText.setVisibility(View.GONE);
                }

            }
        });

    }

}